package com.thiscc.sum.logic;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.thiscc.sum.constance.TableName;
import com.thiscc.sum.dao.CommonDao;
import com.thiscc.sum.utils.JSONUtils;
import com.thiscc.sum.utils.RequestUtils;
import com.thiscc.sum.utils.StringUtils;
import com.thiscc.sum.utils.TableNameConstant;
import com.thiscc.sum.webservice.WebService;

/**
 * 总结分组的业务逻辑处理类
 * @author DK
 * @createTime 2014.12.15
 *
 */
@Component
public class GroupLogic {

    @Autowired
    private CommonDao commonDao;

    /*-------------------------------------------------------------------------------------------*/

    /**
     * 删除分组
     * 
     * @param request
     *         uuid   操作人的uuid
     *         groupname 分组名
     * @return String 1移除成功  2移除失败
     * @author DK
     */
    public String delGroup(HttpServletRequest request) {

        String s_uuid = RequestUtils.getString(request, "uuid");
        if (StringUtils.isBlank(s_uuid) || "null".equals(s_uuid)) {
            s_uuid = (request.getSession().getAttribute("uuid")).toString();
        }
        String s_groupname = RequestUtils.getString(request, "groupname");

        DBObject DBO_condition = new BasicDBObject();
        DBO_condition.put("uuid", s_uuid);
        DBO_condition.put("groupname", s_groupname);

        //数据库操作
        boolean b_bool = commonDao.deleteCommon(TableName.SUMMARIZE_GROUP, DBO_condition);

        return b_bool ? "1" : "2";
    }

    /*-------------------------------------------------------------------------------------------*/

    /**
     * 分组的置顶操作
     * @param request
     *            uuid   登陆人的uuid
     *            groupname  组名
     * @return String 1修改成功 2修改失败
     * @author DK
     */
    public String putMost(HttpServletRequest request) {

        String s_uuid = RequestUtils.getString(request, "uuid");
        if (StringUtils.isBlank(s_uuid) || "null".equals(s_uuid)) {
            s_uuid = (request.getSession().getAttribute("uuid")).toString();
        }
        String s_groupname = RequestUtils.getString(request, "groupname");

        DBObject DBO_condition = new BasicDBObject();
        DBO_condition.put("uuid", s_uuid);
        DBO_condition.put("groupname", s_groupname);

        //需要修改的值
        DBObject DBO_dataBean = new BasicDBObject();
        DBO_dataBean.put("putmost", "1");
        DBO_dataBean.put("time", new Date());

        //数据库操作
        boolean b_bool = commonDao.updateCommon(TableName.SUMMARIZE_GROUP, DBO_condition, DBO_dataBean);

        return b_bool ? "1" : "2";
    }

    /*-------------------------------------------------------------------------------------------*/
    /**
     * 修改分组
     * @param groupBeforeName //拿到修改前分组的名称
     * @param groupAfterName //拿到修改后分组的名称
     * @param uuid //用户uuid
     * @author 易泉 2014-12-15 09:52:00 
     */
    public String updateGroup(HttpServletRequest request) {

        //拿到修改前分组的名称
        String s_groupBeforeName = RequestUtils.getString(request, "groupBeforeName");

        //拿到修改后分组的名称
        String s_groupAfterName = RequestUtils.getString(request, "groupAfterName");

        //拿到用户uuid
        String s_uuid = RequestUtils.getString(request, "uuid");
        if (StringUtils.isBlank(s_uuid) || "null".equals(s_uuid)) {
            s_uuid = (request.getSession().getAttribute("uuid")).toString();
        }

        //查询对象
        DBObject DBO_conditionBean = new BasicDBObject();
        DBO_conditionBean.put("uuid", s_uuid);
        DBO_conditionBean.put("groupname", s_groupBeforeName);

        //修改对象
        DBObject DBO_updateBean = new BasicDBObject();
        DBO_updateBean.put("groupname", s_groupAfterName);
        DBO_updateBean.put("time", new Date());

        //根据分组前的名称跟UUID 来查询一个分组得到一个对象
        boolean b_flag = commonDao.updateCommon(TableNameConstant.T_SUMMARIZE_GROUP, DBO_conditionBean, DBO_updateBean);

        //创建是否修改成功  1为成功  2为失败
        return b_flag ? "1" : "2";
    }

    /*-------------------------------------------------------------------------------------------*/
    /**
     *	创建分组
     * @param groupName //拿到分组的名称
     * @param uuid //用户uuid
     * @param leaguerUidd //组成员的uuid
     * @author 易泉  
     * @createTime 2014-12-15
     */
    public String creatGroup(HttpServletRequest request) {

        //拿到分组的名称
        String s_groupName = RequestUtils.getString(request, "groupName");

        //拿到用户uuid
        String s_uuid = request.getSession().getAttribute("uuid").toString();

        //拿到企业的companyUidd
        String s_companyUidd = RequestUtils.getString(request, "companyUidd");

        //拿到部门的UUID
        String s_deptUidd = RequestUtils.getString(request, "deptUidd");

        //拿到组成员的uuid
        String s_leaguerUidd = RequestUtils.getString(request, "leaguerUidd");

        //定义一个JSONArray 添加查询出来的UUID
        JSONArray JSY_group = new JSONArray();
        Map<String, String> groupMap = new HashMap<String, String>();

        //拿到的是String类型  数据库是数据 要做转换
        JSONArray JSY_leaguer = JSONUtils.getJSONArray(s_leaguerUidd);

        //拿到数组第一个值
        String s_companyno = null;

        for (int i = 0; i < JSY_leaguer.size(); i++) {

            //拿到数组第一个值
            String s_strDept = JSY_leaguer.getString(i);

            //字符串截取
            int i_length = s_strDept.indexOf("*");
            s_companyno = s_strDept.substring(0, i_length);
            JSY_group.add(s_companyno);

            groupMap.put(s_companyno, s_companyno);
        }

        //调用方法通过部门UUID查询部门所有的人的UUID
        JSONArray JSY_deptUidd = null;

        //拿到数组第一个值
        String s_DeptStr = null; //拿到数组第一个值
        JSONArray JSY_dept = null; //把界面传参的部门编号转成数组形式
        String s_deptno = null; //截取后的部门ID
        JSONObject JSY_obj = null; //去掉返回的name  只要UUID

        //判断值是否为空
        if (StringUtils.isNotBlank(s_deptUidd) && StringUtils.isNotBlank(s_companyUidd)) {

            JSY_dept = JSONUtils.getJSONArray(s_deptUidd);
            for (int i = 0, len = JSY_dept.size(); i < len; i++) {

                //拿到数组第一个值
                s_DeptStr = JSY_dept.getString(i);

                //字符串截取
                int i_length = s_DeptStr.indexOf("*");
                s_deptno = s_DeptStr.substring(0, i_length);

                //调用方法 通过部门ID 查询部门人
                JSY_deptUidd = this.getUUIDByframe(s_companyUidd, s_deptno);

                for (int j = 0; j < JSY_deptUidd.size(); j++) {

                    JSY_obj = JSY_deptUidd.getJSONObject(j);
                    JSY_group.add(JSY_obj.getString("uuid"));

                    groupMap.put(JSY_obj.getString("uuid"), JSY_obj.getString("uuid"));
                }
            }
        }

        Set<String> set = groupMap.keySet();

        //置顶状态 默认为0为不置顶  1为置顶
        String s_putmost = "0";

        //插入对象
        DBObject DBO_insertBean = new BasicDBObject();
        DBO_insertBean.put("uuid", s_uuid);
        DBO_insertBean.put("groupname", s_groupName);
        DBO_insertBean.put("group", set);
        DBO_insertBean.put("time", new Date());
        DBO_insertBean.put("putmost", s_putmost);

        //调用方法  普通的插入方法
        boolean b_flag = commonDao.insertCommon(TableName.SUMMARIZE_GROUP, DBO_insertBean);

        //创建是否创建成功  1为成功  2为失败
        return b_flag ? "1" : "2";
    }

    /*-------------------------------------------------------------------------------------------*/

    /**
     * 根据公司编号、部门uuid递归查询，获取该部门下所有人的uuid，name，包括部门下的子部门
     * @param companyno 公司编号
     * @param frameUUID 部门uuid
     * @return
     * @author helios
     */
    private JSONArray getUUIDByframe(String companyno, String frameUUID) {

        WebService ws = new WebService();
        ws.setURL("/businessplat/services/CcAndroidService");
        ws.setMethodName("getCompanyCCByFrame");
        ws.setProperty(new String[] { companyno, frameUUID });

        String result = ws.getString();
        JSONArray UUIDArr = JSONUtils.getJSONArray(result);
        return UUIDArr;
    }
}
